package com.nanyang.tms.service.waybillreceipt.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.waybillreceipt.WaybillCustomerNoQueryRequest;
import com.nanyang.tms.api.dto.request.waybillreceipt.WaybillReceiptFeeQueryRequest;
import com.nanyang.tms.api.dto.request.waybillreceipt.WaybillReceiptQueryRequest;
import com.nanyang.tms.api.dto.request.waybillreceipt.WaybillReceiptTrackQueryRequest;
import com.nanyang.tms.api.dto.response.forecast.ForecastDeclareResponse;
import com.nanyang.tms.api.dto.response.forecast.ForecastReceiverResponse;
import com.nanyang.tms.api.dto.response.forecast.ForecastSenderResponse;
import com.nanyang.tms.api.dto.response.forecast.ForecastVolumeResponse;
import com.nanyang.tms.api.dto.response.waybill.WaybillIncomeFeePageResponse;
import com.nanyang.tms.api.dto.response.waybillreceipt.*;
import com.nanyang.tms.api.service.waybillreceipt.WaybillReceiptRpcService;
import com.nanyang.tms.domain.waybill.*;
import com.nanyang.tms.manager.waybill.WaybillIncomeFeeManager;
import com.nanyang.tms.manager.waybill.WaybillReceiptManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class WaybillReceiptRpcServiceImpl implements WaybillReceiptRpcService {
    @Autowired
    private WaybillReceiptManager waybillReceiptManager;
    @Autowired
    private WaybillIncomeFeeManager waybillIncomeFeeManager;
    @Override
    public ApiResult<PageBean<WaybillReceiptPageResponse>> listWaybillReceipt(WaybillReceiptQueryRequest param) {
        ApiResult<PageBean<WaybillReceiptPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillReceiptQuery query = new WaybillReceiptQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<WaybillReceiptVO> waybillList = waybillReceiptManager.listWaybillReceipt(query);
            PageBean<WaybillReceiptPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(waybillList.getTotalItem());
            BeanUtils.copyListProperties(waybillList.getData(), resultList.getData(), WaybillReceiptPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<WaybillBaseDetailResponse> detailWaybillReceipt(WaybillCustomerNoQueryRequest param) {
        ApiResult<WaybillBaseDetailResponse> result = new ApiResult<>();
        try {
            WaybillBaseDetailResponse waybillBaseDetailResponse = new WaybillBaseDetailResponse();
            WaybillReceiptQuery query = new WaybillReceiptQuery();
            BeanUtils.copyProperties(param, query);
            WaybillDetailVO waybillDetailVO = waybillReceiptManager.selectByCustomerNo(query);

            WaybillReceiptDetailResponse waybillReceiptDetailResponse = new WaybillReceiptDetailResponse();
            BeanUtils.copyProperties(waybillDetailVO.getWaybillReceiptVO(),waybillReceiptDetailResponse);
            waybillBaseDetailResponse.setWaybill(waybillReceiptDetailResponse);

            ForecastReceiverResponse forecastReceiverResponse = new ForecastReceiverResponse();
            BeanUtils.copyProperties(waybillDetailVO.getForecastReceiverVO(),forecastReceiverResponse);
            waybillBaseDetailResponse.setReceiver(forecastReceiverResponse);

            ForecastSenderResponse forecastSenderResponse=new ForecastSenderResponse();
            BeanUtils.copyProperties(waybillDetailVO.getForecastSenderVO(),forecastSenderResponse);
            waybillBaseDetailResponse.setSender(forecastSenderResponse);

            List<ForecastDeclareResponse> declareResponseList=new ArrayList<>();
            BeanUtils.copyListProperties(waybillDetailVO.getForecastDeclareVOList(),declareResponseList,ForecastDeclareResponse.class);
            waybillBaseDetailResponse.setDeclareList(declareResponseList);

            List<ForecastVolumeResponse> volumeResponseList=new ArrayList<>();
            BeanUtils.copyListProperties(waybillDetailVO.getForecastVolumeVOList(),volumeResponseList,ForecastVolumeResponse.class);
            waybillBaseDetailResponse.setVolumeList(volumeResponseList);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(waybillBaseDetailResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<PageBean<WaybillReceiptFeePageResponse>> listWaybillReceiptFee(WaybillReceiptFeeQueryRequest param) {
        ApiResult<PageBean<WaybillReceiptFeePageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillReceiptQuery query = new WaybillReceiptQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<WaybillReceiptVO> waybillList = waybillReceiptManager.listWaybillFee(query);
            PageBean<WaybillReceiptFeePageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(waybillList.getTotalItem());
            BeanUtils.copyListProperties(waybillList.getData(), resultList.getData(), WaybillReceiptFeePageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<PageBean<WaybillReceiptFeeDetailResponse>> listWaybillReceiptFeeDetail(WaybillReceiptFeeQueryRequest param) {
        ApiResult<PageBean<WaybillReceiptFeeDetailResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillIncomeFeeQuery query = new WaybillIncomeFeeQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<WaybillIncomeFeeVO> waybillIncomeFeeList = waybillIncomeFeeManager.listWaybillIncomeFee(query);
            PageBean<WaybillReceiptFeeDetailResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(waybillIncomeFeeList.getTotalItem());
            BeanUtils.copyListProperties(waybillIncomeFeeList.getData(), resultList.getData(), WaybillReceiptFeeDetailResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<PageBean<WaybillReceiptTrackPageResponse>> listWaybillReceiptTrack(WaybillReceiptTrackQueryRequest param) {
        ApiResult<PageBean<WaybillReceiptTrackPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillReceiptQuery query = new WaybillReceiptQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<WaybillReceiptVO> waybillReceiptList = waybillReceiptManager.listWaybillReceipt(query);
            PageBean<WaybillReceiptTrackPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(waybillReceiptList.getTotalItem());
            BeanUtils.copyListProperties(waybillReceiptList.getData(), resultList.getData(), WaybillReceiptTrackPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
}
